<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/29 0029
 * Time: 16:50
 */

namespace core;


class Rand
{
    /**
     * 随机字符生成
     * @param int $lenght 长度
     * @param string $type 随机字符范围 all：特殊字符+字母大小写+数字 letter：字母大小写 upper：字母大写 lower：字母小写 int：数字
     * @return string 随机字符串
     */
    public static function str($lenght = 6, $type = "")
    {
        $str_pol = "";
        $n = 0;
        switch ($type) {
            case "all" :
                $str_pol .= "~!@#$%&*()_+|-=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
                break;
            case "letter" :
                $str_pol .= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
                break;
            case "upper" :
                $str_pol .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                break;
            case "lower" :
                $str_pol .= "abcdefghijklmnopqrstuvwxyz";
                break;
            case "int" :
                $str_pol .= "0123456789";
                break;
            default :
                $str_pol .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        }
        $len = strlen($str_pol);
        if ($len < $lenght) {
            $n = ceil($lenght/$len);
            for ($i = 1; $i < $n; $i++) {
                $str_pol .= $str_pol;
            }
        }
        return substr(str_shuffle($str_pol), 0, $lenght);
    }
}